//
//  WBPresentationManager.swift
//  weibo
//
//  Created by Felix on 16/11/7.
//  Copyright © 2016年 Felix. All rights reserved.
//

import UIKit




class WBPresentationManager:NSObject, UIViewControllerTransitioningDelegate{
    
    //MARK:-UIViewControllerTransitioningDelegate
    
    //定义一个标记、是否为弹出
    var isPresent = false
    
    ///定义一个变量、用于记录弹出框的frame
    var presentFrame:CGRect = CGRect()

    
    // 该方法用于返回一个负责转场动画的对象
    // 可以在该对象中控制弹出视图的尺寸等
    func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController?{
        ///创建一个弹出视图对象
        let pop =  WBPopOverPresentationController.init(presentedViewController: presented, presenting: presenting)
        ///给弹出视图设置frame
        pop.presentFrame = presentFrame
        return pop
    }
    
    // 该方法用于返回一个负责转场如何出现的对象
    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning?{
        isPresent = true
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: WBPresentationManagerDidPresented), object: nil)
        return self
    }
    
    // 该方法用于返回一个负责转场如何消失的对象
    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning?{
        isPresent = false
        NotificationCenter.default.post(name: NSNotification.Name(rawValue:WBPresentationManagerDidDismissed), object: nil)
        return self
    }
    
    
    
    
}
extension WBPresentationManager:UIViewControllerAnimatedTransitioning{
    //MARK:- UIViewControllerAnimatedTransitioning
    //用于定义动画出现和消失的统一时长
    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval{
        return 0.3
    }
    
    
    // 专门用于管理modal如何展现和消失的, 无论是展现还是消失都会调用该方法
    /*
     注意点: 只要我们实现了这个代理方法, 那么系统就不会再有默认的动画了
     也就是说默认的modal从下至上的移动系统不帮再帮我们添加了, 所有的动画操作都需要我们自己实现, 包括需要展现的视图也需要我们自己添加到容器视图上(containerView)
     */
    // transitionContext: 所有动画需要的东西都保存在上下文中, 换而言之就是可以通过transitionContext获取到我们想要的东西
    func animateTransition(using transitionContext: UIViewControllerContextTransitioning){
        
        if isPresent {
          willPresentedViewController(using: transitionContext)
        }else{
           willDismissedViewController(using: transitionContext)
        }
    }
    
    private func willPresentedViewController(using transitionContext: UIViewControllerContextTransitioning ){
        guard let toView = transitionContext.view(forKey: UITransitionContextViewKey.to) else {
            return
        }
        
        transitionContext.containerView.addSubview(toView)
        ///1.让视图高度为0
        toView.transform = CGAffineTransform.init(scaleX: 1.0, y: 0)
        ///2.设置动画锚点为x轴中间、y轴为0
        toView.layer.anchorPoint = CGPoint(x: 0.5, y: 0.0)
        
        UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {
            ///3.还原视图高度（慢慢展开）
            toView.transform = CGAffineTransform.identity
        }) { (true) in
            ///4.动画执行完结束
            transitionContext.completeTransition(true)
        }
    }
    
    private func willDismissedViewController(using transitionContext: UIViewControllerContextTransitioning ){
        guard let fromView = transitionContext.view(forKey: UITransitionContextViewKey.from) else {
            return
        }
        UIView.animate(withDuration:transitionDuration(using: transitionContext), animations: {
            fromView.transform = CGAffineTransform.init(scaleX: 1.0, y: 0.0001)
            }, completion: { (true) in
                transitionContext.completeTransition(true)
        })
    }

}




